package com.hl.card.modular.packBack.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import com.hl.card.modular.pack.service.TokenWallet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.hl.card.common.constant.tips.SuccessTip;
import com.hl.card.common.controller.BaseController;
import com.hl.card.common.persistence.model.Recharge;
import com.hl.card.common.persistence.model.ckeck.CurrencyUpdateCheck;
import com.hl.card.common.persistence.model.ckeck.RechargeDocCheck;
import com.hl.card.core.log.LogObjectHolder;
import com.hl.card.core.shiro.ShiroKit;
import com.hl.card.modular.pack.service.RechargeService;

@Controller
@RequestMapping("/currencyList")
public class CurrencyInfoController extends BaseController {
    private String PREFIX = "/packback/currencyInfo/";
    private final String EXTRA = "WITHDRAW";
    private final String INDEX_PAGE = "/currencyList";
    private final String LIST_PAGE = "currencyList.html";
    private final String EDIT_PAGE = "currencyList_edit.html";
    @Autowired
    private RechargeService rechargeService;
	@Autowired
	TokenWallet tokenWallet;

    @RequestMapping("")
    public String index(HttpServletRequest request, ModelMap model){
    	String toPage=INDEX_PAGE;
		model.put("notify", "提现审核操作");
    	String page =baseCheckAccess(request, toPage, "/currencyList", model);
//    	String page = toPage;
    	if (!toPage.equals(page)) {
    		return page;
		}
    	return PREFIX+LIST_PAGE;
    }


    /**
     * 跟单
     */
    @RequestMapping(value = "/doc")
    @ResponseBody
    public Object doc(@Validated(value=RechargeDocCheck.class)Recharge recharge){
    	recharge.setExtra(EXTRA);
    	return rechargeService.doc(recharge);
    }

    /**
     * 跳转到修改申请页面
     */
    @RequestMapping(value = "/currency_update/{recharge_no}")
    public Object joinRebateUpdate(@PathVariable String recharge_no, ModelMap model, HttpServletRequest request){
    	String toPage=PREFIX+EDIT_PAGE;
    	String page =baseCheckAccess(request, toPage, model);
//    	String page = toPage;
    	if (!toPage.equals(page)) {
    		return page;
		}
    	Recharge recharge= new Recharge(recharge_no);
    	recharge.setExtra(EXTRA);
    	Map<String, Object> map = rechargeService.select(recharge);
    	
    	model.addAttribute("currencyList",map);
    	LogObjectHolder.me().set(map);
    	
        return toPage;
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Recharge recharge,Integer pageSize, Integer pageIndex) {
    	recharge.setExtra(EXTRA);
    	Page<Recharge> dPage = rechargeService.selectMultiCondition(recharge);
    	Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", dPage.getTotal());
		jsonMap.put("rows", dPage.getRecords());
        return jsonMap;
    }
    
    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Validated(value=CurrencyUpdateCheck.class)Recharge recharge){
    	SuccessTip su = new SuccessTip();
    	Recharge rechargeSelect= new Recharge();
    	rechargeSelect.setDoc_account(ShiroKit.getUser().getAccount());
    	rechargeSelect.setRecharge_no(recharge.getRecharge_no());
    	Recharge tempRecharge= rechargeService.selectMultiConditionOne(rechargeSelect);
    	if (tempRecharge==null || tempRecharge.getStatus()==3) {
			su.setMessage("无法操作");
			su.setCode(2);
		}else {
			JSONObject json = new JSONObject();
	        json.put("mchTransNo", recharge.getRecharge_no());
	        JSONObject resObj = rechargeService.confirmTrans(tempRecharge);
			if("SUCCESS".equals(resObj.getString("retCode")) && "SUCCESS".equalsIgnoreCase(resObj.getString("resCode").toString())) {
				//支付中心提币成功，开始调用token钱包服务的自动提币接口
				recharge.setAudit_time(new Date());
				recharge.setStatus(4);
				recharge.setConfirm_transfer_time(recharge.getConfirm_transfer_time());
				recharge.setMiner(recharge.getMiner());
				//recharge.setPoundage(2d);
				rechargeService.auditing(recharge);
			}else {
				su.setMessage("操作失败");
				su.setCode(2);
			}
		}
    	return su;
    }


}
